package com.myz.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

// 高级发布订阅 + 备份交换机 配置文件
@Configuration
public class ConfirmConfig {
    // 交换机
    public static final String CONFIRM_EXCHANGE = "confirm_exchange";
    // queue
    public static final String CONFIRM_QUEUE = "confirm_queue";
    public static final String CONFIRM_ROUTINGKEY = "confirm_key";

    // 备份交换机
    public static final String BACKUP_EXCHANGE = "backup_exchange";
    // 备份队列queue
    public static final String BACKUP_QUEUE = "backup_queue";
    // 报警队列
    public static final String WARNING_QUEUE = "warning_queue";
    // 声明交换机
    @Bean
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).durable(true).withArgument("alternate-exchange",BACKUP_EXCHANGE).build();
    }
    // 声明队列
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }
    // 声明备份交换机 交换机类型为扇出类型 扇出类型不需要routingKey
    @Bean("backupExchange")     // 此处如果不起名字,默认为方法名
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE);
    }
    // 声明备份队列
    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }
    // 备份队列绑定交换机
    @Bean
    public Binding backupQueueBinding(@Qualifier("backupExchange") FanoutExchange backupExchange,
                                      @Qualifier("backupQueue") Queue backupQueue){
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }
    // 声明报警队列
    @Bean
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }
    // 报警队列绑定交换机
    @Bean
    public Binding waringQueueBinding(@Qualifier("backupExchange") FanoutExchange backupExchange,
                           @Qualifier("warningQueue") Queue warningQueue){
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }


    // 队列绑定交换机
    @Bean
    public Binding queueBE(@Qualifier("confirmExchange") DirectExchange confirmExchange,
                                        @Qualifier("confirmQueue") Queue confirmQueue){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTINGKEY);
    }
}
